﻿@page "/user"
@using BlazorDemoCURD.Models
@using Microsoft.EntityFrameworkCore;
@using BlazorDemoCURD.DataBase
@implements IDisposable
  @*Dispose接口实现，才能每个组件结束时关闭DbContext线程*@
@inject IDbContextFactory<MyDbContext> DbFactory 
  @*注入DbContext*@

<Table TItem="UserInfo"
       IsPagination="true"
       IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowSkeleton="true"
       OnQueryAsync="@OnEditQueryAsync" OnResetSearchAsync="@OnResetSearchAsync"
       OnAddAsync="@OnAddAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.Id" Width="180" />
        <TableColumn @bind-Field="@context.Name" />
        <TableColumn @bind-Field="@context.Age" />
        <TableColumn @bind-Field="@context.PhoneNum" />
    </TableColumns>
    <EditTemplate>
        <div class="form-inline">
            <div class="row">
                <div class="form-group col-12 col-sm-6">
                    <BootstrapInput @bind-Value="@context.Name" placeholder="不可为空，50字以内" maxlength="50">
                        <RequiredValidator />
                        <StringLengthValidator Length="50" />
                    </BootstrapInput>
                </div>
                <div class="form-group col-12 col-sm-6">
                    <BootstrapInput @bind-Value="@context.Age" placeholder="不可为空，50字以内" maxlength="50">
                        <RequiredValidator />
                        <StringLengthValidator Length="50" />
                    </BootstrapInput>
                </div>
                <div class="form-group col-12 col-sm-6">
                    <BootstrapInput @bind-Value="@context.PhoneNum" placeholder="不可为空，50字以内" maxlength="50">
                        <RequiredValidator />
                        <StringLengthValidator Length="50" />
                    </BootstrapInput>
                </div>
            </div>
        </div>
    </EditTemplate>
</Table>


@code {
    private MyDbContext Context { get; set; } 

    protected override async Task OnInitializedAsync()//这里相当于是构造方法
    {
        Context = DbFactory.CreateDbContext();
        await base.OnInitializedAsync();
    }

    

    protected async Task<UserInfo> OnAddAsync()   
    {
        return await Task.FromResult(new UserInfo());
    }


    protected async Task<bool> OnSaveAsync(UserInfo item)
    {
        //数据保存
        await Context.AddAsync(item);
        await Context.SaveChangesAsync();
        return await Task.FromResult(true);
    }

    protected async Task<bool> OnDeleteAsync(IEnumerable<UserInfo> items)
    {//这里删除方法，没有写
        return await Task.FromResult(true);
    }

    protected async Task<QueryData<UserInfo>> OnEditQueryAsync(QueryPageOptions options)
    {
        var query = Context.UserInfos;
        var ret = new QueryData<UserInfo>()
        {
            TotalCount = query.Count(),
            Items = query
        };
        return await Task.FromResult(ret);
    }

    protected Task OnResetSearchAsync(UserInfo item)
    {
        item.Name = "";
        item.PhoneNum = "";
        return Task.CompletedTask;
    }

    //释放DbContext
    public void Dispose()
    {
        Context.Dispose();
    }
}
